<template>
  <div class="container">
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="所属学科" prop="subjectID" v-if="!this.$route.query.id">
        <el-select v-model="form.subjectID" placeholder="请选择活动区域">
          <el-option
            :label="item.label"
            :value="item.value"
            v-for="item in directoryName"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="form.directoryName"></el-input>
      </el-form-item>

      <el-form-item size="large">
        <el-button @click="cancelFn">取消</el-button>
        <el-button type="primary" @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { simple as desList } from '@/api/hmmm/subjects'
import { add as addDes, detail, update } from '@/api/hmmm/directorys'
export default {
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },
    pid: {
      type: Object,
      required: true
    },
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      form: {
        subjectID: '',
        directoryName: ''
      },
      rules: {
        directoryName: [
          { required: true, message: '所属学科不能为空', trigger: 'blur' }
        ]
      },
      directoryName: [] // 学科列表
    }
  },
  methods: {
    // 提交
    onSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 通过
          if (this.isEdit) {
            this.doEdit()
          } else {
            this.addFn()
          }
        } else {
          alert('error')
        }
      })
      console.log(this.form)
    },
    // 获取学科列表
    async loadDesList () {
      const res = await desList()
      //   console.log(res)
      this.directoryName = res.data
      console.log(this.directoryName)
    },
    // 取消功能
    cancelFn () {
      this.form = {
        subjectID: '',
        directoryName: ''
      }
      this.$emit('closeDialog')
    },
    // 添加功能
    async addFn () {
      try {
        if (this.$route.query.id) {
          const res = await addDes({ directoryName: this.form.directoryName, subjectID: Number(this.$route.query.id) })
          console.log(res)
        } else {
          const res = await addDes(this.form)
          console.log(res)
        }
        this.$emit('closeDialog')
      } catch (err) {
        this.$message(err.message)
      }
    },
    // 编辑
    async doEdit () {
      const res = await update(this.form)
      console.log(res)
      this.$emit('closeDialog')
    }
  },
  async created () {
    this.loadDesList()
    // 如果是编辑就要去回去详情
    if (this.isEdit) {
      // 获取详情
      const res = await detail(this.pid)
      console.log(res)
      this.form = res.data
    }
  }
}
</script>

<style scoped>
.bg-purple-light {
  background: #d9dadc;
  line-height: 30px;
  border-radius: 5px;
  padding-left: 10px;
  font-size: 12px;
  color: rgb(132, 127, 127);
}
</style>
